<template>
  <div class="user-manage">
    <div class="head" style="margin-bottom:20px">
      <div class="left">
        <el-button type="primary" @click="userAddFormVisible = true"
          >增加新用户</el-button
        >
      </div>
      <div class="right">
        <el-input
          v-model="searchText"
          placeholder="请输入用户手机号"
        ></el-input>
        <el-button type="primary" @click="queryUser">搜索</el-button>
        <el-button type="primary" @click="getAllUser">显示全部</el-button>
      </div>
    </div>
    <hr />
    <el-dialog
      title="增加新用户"
      :visible.sync="userAddFormVisible"
      width="50%"
      :before-close="handleClose"
    >
      <user-add-form></user-add-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="userAddFormVisible = false">取 消</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="编辑用户信息"
      :visible.sync="userUpdateFormVisible"
      width="50%"
      :before-close="handleClose"
    >
      <user-update-form :inf="userUpdateData"></user-update-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="userUpdateFormVisible = false">取 消</el-button>
      </span>
    </el-dialog>
    <div class="table">
      <el-table :data="tableData" style="width: 100%" stripe height="400">
        <el-table-column label="ID" width="110">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column label="姓名" width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.name }}</span>
          </template> </el-table-column
        ><el-table-column label="手机号" width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.phone }}</span>
          </template> </el-table-column
        ><el-table-column label="密码" width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.password }}</span>
          </template>
        </el-table-column>
        <el-table-column label="账号类型" width="110">
          <template slot-scope="scope">
            <span style="margin-left: 10px" v-if="scope.row.type == 0"
              >普通用户</span
            >
            <span style="margin-left: 10px" v-if="scope.row.type == 1"
              >管理员</span
            >
          </template>
        </el-table-column>
        <el-table-column label="账号状态" width="110">
          <template slot-scope="scope">
            <span style="margin-left: 10px" v-if="scope.row.status == 0"
              >禁用</span
            >
            <span style="margin-left: 10px" v-if="scope.row.status == 1"
              >正常</span
            >
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="danger" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>


<script>
import UserAddForm from './UserAddForm.vue';
import UserUpdateForm from './UserUpdateForm.vue';
export default {
  name: "UserManage",
  components: {
    UserAddForm,
    UserUpdateForm,
  },
  data() {
    return {
      tableData: "", //表格数据
      searchText: "", //搜索框内容
      userAddFormVisible: false, //打开增加用户窗口
      userUpdateFormVisible:false,//打开编辑用户窗口
      userUpdateData:""//传送给编辑用户表单的数据
    };
  },
  methods: {
    // 获取所有用户API
    getAllUser() {
      let _this = this;
      this.$axios({
        url: "/user/getAllUser",
        method: "post",
      }).then(function (resp) {
        _this.tableData = resp.data;
      });
    },
    // 搜索用户
    queryUser() {
      let _this = this;
      this.$axios({
        url: "/user/queryUser",
        method: "post",
        data: { phone: _this.searchText },
      }).then(function (resp) {
        if (resp.data == "") {
          _this.tableData = "";
        } else {
          _this.tableData = [resp.data];
        }
      });
    },

    // 修改用户信息
    handleEdit(index, row) {
      console.log(index);
      console.log(row.name);
      // alert(row)
      this.userUpdateData = {
        oldPhone: row.phone,
        user: JSON.parse(JSON.stringify(row))
      }
      this.userUpdateFormVisible = true
    },
    
  },
  created() {
    this.getAllUser();
  },
};
</script>


<style lang="less" scoped>
.user-manage {
  .head {
    display: flex;
    justify-content: space-between;
    .right {
      display: flex;
    }
  }
}
</style>

